//
//  HMOAuthController.swift
//  weibo
//
//  Created by 杨念川 on 16/9/5.
//  Copyright © 2016年 杨念川. All rights reserved.
//

import UIKit
import SVProgressHUD
//授权所需要的一些信息
let WB_APPKEY = "3520527295"
let WB_APPSECRET = "6cea4c7cc9aa7e1e3f016a28a852a0d0"
let WB_URL = "http://www.itheima.com/"

class HMOAuthController: UIViewController,UIWebViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", target: self, action: #selector(back))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: #selector(addFill))
        setupUI()
        // Do any additional setup after loading the view.
    }
    //初始化UI
    private func setupUI() {
        self.view.backgroundColor = UIColor.white
        //创建webView
        let webView = UIWebView()
        self.view.addSubview(webView)
        //webView自动布局
        webView.snp_makeConstraints { (maeke) in
            maeke.edges.equalTo(self.view)
        //请求地址
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(WB_APPKEY)&redirect_uri=\(WB_URL)"
        //请求的URL
        let url = URL(string: urlString)
        //请求
        let request  = URLRequest(url: url!)
        //发送请求
        webView.loadRequest(request)
        }
    }
    //左边按钮的点击事件
    func back() {
        dismiss(animated: true, completion: nil)
    }
    //自动填充
    @objc private func addFill() {
        // 去执行js代码
        let jsString = "document.getElementById('userId').value = 'y0801y@126.com';document.getElementById('passwd').value = 'chuan6477082';"
        webView.stringByEvaluatingJavaScript(from: jsString)
    }
    //懒加载
    private lazy var webView: UIWebView = {
        let webView = UIWebView()
        webView.delegate = self
        return webView
    }()
    //MARK: - UIWebView的代理方法
    //已经开始加载的时候
    func webViewDidStartLoad(_ webView: UIWebView) {
        //显示指示器
        SVProgressHUD.show()
    }
    //加载失败的时候
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        //移除指示器
        SVProgressHUD.dismiss()
    }
    //加载成功的时候
    func webViewDidFinishLoad(_ webView: UIWebView) {
        //移除指示器
        SVProgressHUD.dismiss()
    }
    //应该加载WebView
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //判断url是不是以回调页开头
        if let u = request.url {
            let urlString = u.absoluteString
            if !urlString.hasPrefix(WB_URL) {
                return true
            }
        }
        if let query = request.url?.query {
            //获取code
            let code = query.substring(from: "code=".endIndex)
            print(code)
            
        }
        return false
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
